package com.example.luozizhao.eprxdemo.base.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.example.luozizhao.eprxdemo.base.presenter.BasePresenter;
import com.example.luozizhao.eprxdemo.base.view.BaseView;
import com.example.luozizhao.eprxdemo.commom.ActivityControl;
import com.example.luozizhao.eprxdemo.di.component.ActivityComponent;
import com.example.luozizhao.eprxdemo.di.component.DaggerActivityComponent;
import com.example.luozizhao.eprxdemo.di.module.ActivityModule;
import com.example.luozizhao.eprxdemo.utils.CommonUtils;


import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by luozizhao on 2018/5/16.
 */

public abstract class BaseActivity<T extends BasePresenter> extends SupportActivity implements BaseView{
    private int layoutId;
    private Unbinder mBind;
    @Inject protected T mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mBind = ButterKnife.bind(this);
        onViewCreate();
        ActivityControl.getInstance().addActivity(this);
        initData();
    }

    protected void onViewCreate() {
        initInject();
        if (mPresenter!=null) {
            mPresenter.attach(this);
        }
    }


    @Override
    protected void onDestroy() {
        if (mPresenter!=null) {
            mPresenter.detach();
        }
        mBind.unbind();
        ActivityControl.getInstance().removeActivity(this);
        super.onDestroy();
    }


    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder().activityModule(new ActivityModule()).build();
    }


    @Override
    public void useNightMode(boolean isNightMode) {

    }

    @Override
    public void showErrorMsg(String errorMsg) {
        ToastUtils.showShort(errorMsg);
    }

    @Override
    public void showSnackErrorMsg(Activity activity,String errorMsg) {
        CommonUtils.showSnackMessage(activity,errorMsg);
    }

    @Override
    public void showError() {
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void reload() {

    }

    @Override
    public void showLoginView() {

    }

    @Override
    public void showLogoutView() {

    }

    @Override
    public void showConnectFail() {

    }

    @Override
    public void showCancelConnectFail() {

    }

    @Override
    public void showConnectSuccess() {

    }

    /**
     * 布局layout
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 注入当前Activity所需的依赖
     */
    protected abstract void initInject();
}
